package gqz.testdemo.Bluetooth;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import gqz.testdemo.R;

/**
 * Created by Administrator on 2018/8/15.
 */

public class BleDevAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<BluetoothDevice> list = new ArrayList<>();

    public BleDevAdapter(Context context, ArrayList<BluetoothDevice> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_text, null);
            holder.tv = convertView.findViewById(R.id.item_text_tv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list.size() > 0) {
            BluetoothDevice dev = list.get(position);
            String name = dev.getName() == null ? "null" : dev.getName();
            int bondState = dev.getBondState();
            String bond = bondState == BluetoothDevice.BOND_BONDED ? "已配对" : "";
            holder.tv.setText(name + " | " + dev.getAddress() + " | " + bond);
        }
        return convertView;
    }

    class ViewHolder {
        TextView tv;
    }
}
